package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.CollectDao;
import com.qf.lanzhou.blog.api.pojo.entity.Collect;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author jian
 * @Date 2020/12/17 15:34
 * @JDK 1.8
 * @Version 1.0
 */
public class CollectDaoimpl implements CollectDao {
    private QueryRunner queryRunner=new QueryRunner(DruidUtil.getDataSource());
    @Override
    public List<Collect> select(String collectStatus) {
        String sql = "SELECT * from t_collect where collect_status=1";
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);
        try {
            return queryRunner.query(sql,new BeanListHandler<Collect>(Collect.class, processor));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int insert(String userId, String articleId, String collectStatus) {
        String sql="INSERT into t_collect (user_id,article_id,collect_status) VALUES (?,?,?)";
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);

        Object[] params = {userId, articleId,collectStatus};
        try {
            return queryRunner.update(sql,params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
